﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SundayWishBLL;
using SundayWishModel.Entities;
public partial class User_privatedish : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ContentPlaceHolder UserMaster = (ContentPlaceHolder)Page.Master.Master.FindControl("ContentPlaceHolder1");
            ((Label)UserMaster.FindControl("lbpagename")).Text = "个人食谱详情";
            ((HyperLink)UserMaster.FindControl("hlprivatedishlist")).CssClass = "select_page";
            if (Request["DishId"] != null)
            {
                int DishId = Convert.ToInt32(Request["DishId"]);
                ViewState["DishId"] = DishId;
                PrivateDishHelper pdh = new PrivateDishHelper();
                Dish dish = pdh.GetDish(DishId);
                UserInfo user = (UserInfo)Session["user"];
                if (dish.UserId != user.UserId)
                {
                    Response.Redirect("privatedishlist.aspx");
                }
                else
                {
                    lbCount.Text = dish.DishCount.ToString();
                    lbKind.Text = dish.DishKind;
                    lbName.Text = dish.DishName;
                    img.ImageUrl = "~/image/" + dish.DishImage;
                    lbMaterial.Text = dish.DishMaterial;
                    lbintroduction.Text = dish.DishIntroduction;
                    lbstep.Text = dish.DishStep;
                }
                planlistbind();
            }
            else
            {
                Response.Redirect("privatedishlist.aspx");
            }
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Session["currentplan"]!= null)
        {
            PlanHelper ph = new PlanHelper();
            if(ph.AddDishToPlan(Convert.ToInt32(ViewState["DishId"]),
                Convert.ToInt32(Session["currentplan"]))) WebHelper.AlertMsg("添加", "添加成功");
            else WebHelper.AlertMsg("添加失败", "食谱已存在");
            planlistbind();
        }
        else
        {
            clearplanlist();
            WebHelper.AlertMsg("食谱计划", "请选择食谱计划");
        }

    }
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("privatedishlist.aspx");
    }

    private void planlistbind()
    {
        if (Session["currentplan"] != null)
        {
            PlanHelper ph = new PlanHelper();
            Plan plan = ph.GetPlan(Convert.ToInt32(Session["currentplan"]));
            if (plan != null)
            {
                gvCurrentPlan.DataSource = plan.Dishes;
                gvCurrentPlan.DataBind();
                lbdate.Text = "当前计划日期：" + plan.SendDate.ToLongDateString();
                lbweek.Text = WebHelper.DateToWeek(plan.SendDate);
            }
            else
            {
                clearplanlist();
            }
        }
    }

    private void clearplanlist()
    {
        gvCurrentPlan.DataSource = null;
        gvCurrentPlan.DataBind();
        lbdate.Text = "尚未选择食谱计划";
        lbweek.Text = "";
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("delData"))
        {
            if (Session["currentplan"] != null)
            {
                int DishId = Convert.ToInt32(e.CommandArgument);
                PlanHelper ph = new PlanHelper();
                ph.DeleteDishFromPlan(DishId,Convert.ToInt32(Session["currentplan"]));
            }
        }
        planlistbind();
    }
}
